/**
 * @Title: DeviceController.java
 * @Package me.springboot.mybatis.web
 * @Description: TODO
 * @author zengh
 * @date 2018/04/15
 * @version V1.0
 */
package me.springboot.mybatis.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.springboot.mybatis.common.utils.RoleType;
import me.springboot.mybatis.common.utils.SessionUtil;
import me.springboot.mybatis.common.utils.StringUtil;
import me.springboot.mybatis.core.RetCode;
import me.springboot.mybatis.core.RetResponse;
import me.springboot.mybatis.core.RetResult;
import me.springboot.mybatis.exception.ResultException;
import me.springboot.mybatis.model.Device;
import me.springboot.mybatis.model.User;
import me.springboot.mybatis.service.DeviceService;
import me.springboot.mybatis.service.UserService;
import me.springboot.mybatis.service.impl.XunjianRecordServiceImpl;
import me.springboot.mybatis.web.req.DeviceReq;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zengh
 * @ClassName: DeviceController
 * @Description: TODO
 * @date 2018/04/15
 */
@RestController
@RequestMapping("/device")
@CrossOrigin
public class DeviceController {

    private static final Logger logger = Logger.getLogger(DeviceController.class);

    @Resource
    private DeviceService deviceService;

    @Resource
    private UserService userService;

    /**
     * @param device
     * @Title: add
     * @Description: 手机端添加设备
     * @Reutrn RetResult<Object>
     */
    @PostMapping(value = "/add")
    public RetResult<Object> add(HttpServletRequest request, Device device) throws ResultException {
        User user = SessionUtil.getSessionUser(request);
        if (!user.getType().equals("2") || user.getIsAdd() != 1 || StringUtil.isBlank(user.getKehuId())) {
            throw new ResultException(RetCode.NOT_RIGHT.getCode(), "没有权限");
        }
        logger.info("======+++++++++Device:" + device.toString());
        device.setDeviceStatus(3);
        device.setKehuUserid(user.getKehuId());
        deviceService.addDevice(device);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param device
     * @Title: add2
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/add2")
    public RetResult<Object> add2(@RequestBody Device device) {
        deviceService.save(device);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param id
     * @Title: delete
     * @Description: 删除
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/delete")
    public RetResult<Object> delete(@RequestParam Integer id) {
        deviceService.deleteById(id);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param device
     * @Title: update
     * @Description: 更新
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update")
    public RetResult<Object> update(Device device) {
        deviceService.update(device);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param device
     * @Title: update
     * @Description: 更新(传入参数为JSON格式)
     * @Reutrn RetResult<Object>
     */
    @PostMapping("/update2")
    public RetResult<Object> update2(@RequestBody Device device) {
        deviceService.update(device);
        return RetResponse.makeOKRsp();
    }

    /**
     * @param id
     * @Title: detail
     * @Description: 根据ID查询详情
     * @Reutrn RetResult<Device>
     */
    @PostMapping("/detail")
    public RetResult<Device> detail(@RequestParam Integer id) {
        Device device = deviceService.findById(id);
        return RetResponse.makeOKRsp(device);
    }

    /**
     * @param reqMap
     * @Title: detail
     * @Description: 传入参数为JSON格式
     * @Reutrn RetResult<Device>
     */
    @PostMapping("/detail2")
    public RetResult<Device> detail(@RequestBody Map<String, Object> reqMap) {
        Integer id = Integer.valueOf(reqMap.get("id").toString());
        Device device = deviceService.findById(id);
        return RetResponse.makeOKRsp(device);
    }

    /**
     * @param page 页码
     * @param size 每页条数
     * @Title: list
     * @Description: 分页查询
     * @Reutrn RetResult<PageInfo<Device>>
     */
    @PostMapping("/list")
    public RetResult<PageInfo<Device>> list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Device> list = deviceService.findAll();
        PageInfo<Device> pageInfo = new PageInfo<Device>(list);
        return RetResponse.makeOKRsp(pageInfo);
    }

    @RequestMapping("search")
    public RetResult<PageInfo<Map<String, Object>>> getAll(HttpServletRequest request, @RequestParam(defaultValue = "0") Integer page,
                                              @RequestParam(defaultValue = "0") Integer size, DeviceReq deviceReq) throws ResultException {
        PageHelper.startPage(page, size);
        List<Map<String,Object>> devices = new ArrayList<>();
        if (RoleType.isXunJian(request)) {
            deviceReq.setXunjian_userid(SessionUtil.getSessionUser(request).getuId());
            devices = deviceService.searchXunJianDevice(deviceReq);
        } else if (RoleType.isKehu(request)) {
            deviceReq.setKehu_userid(SessionUtil.getSessionUser(request).getuId());
            devices = deviceService.searchDevice(deviceReq);
        } else{
            devices = deviceService.searchDevice(deviceReq);
        }

        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(devices);
        return RetResponse.makeOKRsp(pageInfo);
    }

    /**
     * 可派单设备
     *
     * @param page
     * @param size
     * @param deviceReq
     * @return
     */
    @RequestMapping("paiDanList")
    public RetResult<PageInfo<Map<String,Object>>> paiDanList(HttpServletRequest request, @RequestParam(defaultValue = "0") Integer page,
                                                  @RequestParam(defaultValue = "20") Integer size, DeviceReq deviceReq) throws ResultException {
        PageHelper.startPage(page, size);
        if (RoleType.isKehu(request)) {
            deviceReq.setKehu_userid(SessionUtil.getSessionUser(request).getuId());
        }
        List<Map<String,Object>> devices = deviceService.paiDanList(deviceReq);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(devices);
        return RetResponse.makeOKRsp(pageInfo);
    }

    /**
     * 客户分配机器列表
     *
     * @param request
     * @param page
     * @param size
     * @param deviceReq
     * @return
     * @throws ResultException
     */
    @RequestMapping("notFenPeiDeviceList")
    public RetResult<PageInfo<Device>> notFenPeiDeviceList(HttpServletRequest request, @RequestParam(defaultValue = "0") Integer page,
                                                           @RequestParam(defaultValue = "20") Integer size, DeviceReq deviceReq) throws ResultException {
        PageHelper.startPage(page, size);
        if (RoleType.isKehu(request)) {
            deviceReq.setKehu_userid(SessionUtil.getSessionUser(request).getuId());
        }
        List<Device> devices = deviceService.notFenPeiDeviceList(deviceReq);
        PageInfo<Device> pageInfo = new PageInfo<Device>(devices);
        return RetResponse.makeOKRsp(pageInfo);
    }

    /**
     * 给巡检人分配设备
     *
     * @param request
     * @param devices
     * @param userid
     * @return
     */
    @RequestMapping("fenPeiXunJianDevice")
    public RetResult<Object> fenPeiXunJianDevice(HttpServletRequest request, @RequestParam(required = true) String devices,
                                                 @RequestParam(required = true) String userid) throws ResultException {
        User currentUser = SessionUtil.getSessionUser(request);

        Map<String, Object> querMap = new HashMap<>();
        querMap.put("uId", userid);
        querMap.put("kehu_id", currentUser.getuId());
        List<User> xunjianUser = userService.selectByMap(querMap);
        if (null == xunjianUser || xunjianUser.isEmpty()) {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "巡检人不存在");
        }

        deviceService.fenPeiXunJianDevice(devices, userid);
        return RetResponse.makeOKRsp(devices);
    }

    @RequestMapping("getCount")
    public RetResult<Map<String, Object>> getCount(HttpServletRequest request) throws ResultException {
        Map<String, Object> paramMap = new HashMap<>();
        if(RoleType.isKehu(request)) {
            paramMap.put("kehu_userid", SessionUtil.getSessionUser(request).getuId());
        }
        Map<String, Object> resultMap = deviceService.getCount(paramMap);
        return RetResponse.makeOKRsp(resultMap);
    }


    @RequestMapping("search2")
    public RetResult<List<Device>> search2(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size, Device device) {
        List<Device> devices = deviceService.selectByModel(device);
        return RetResponse.makeOKRsp(devices);
    }
}